package ProtocolXML;

import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import org.jdom2.* ;
import org.jdom2.input.SAXBuilder;
import org.jdom2.output.XMLOutputter;


public class EntityXML {

    //Need for sending
    private Document document;
    //For service informations
    private Element header;
    //For other informations
    private Element body;

    
    //Constants for operations with XML tags
    private static final String MESSAGE = "Message";
    private static final String HEADER = "Header";
    protected static final String TYPE = "Type";
    private static final String BODY = "Body";

    protected EntityXML() {
        //Create root element
        Element root = new Element(MESSAGE);
        //Create document
        setDocument(new Document(root));
        //Create header and add his in document
        setHeader(new Element(HEADER));
        root.addContent(getHeader());
        //Add type to Header
        getHeader().addContent(new Element(TYPE));
        //Add body
        setBody(new Element(BODY));
        root.addContent(getBody());
    }

    protected EntityXML(Document document) {
        setDocument(document);
        Element root = document.getRootElement();
        setHeader(root.getChild(HEADER));
        setBody(root.getChild(BODY));
    }
    // Write object in stream
    public void write(OutputStream oStream) throws IOException {
        ObjectOutputStream objectOutput = new ObjectOutputStream(oStream) ;
        objectOutput.writeObject(getDocument()) ;
    }
    

    
//*************GETTERS AND SETTERS************************
    private Document getDocument() {
        return document;
    }
    private void setDocument(Document document) {
        this.document = document;
    }
    protected Element getHeader() {
        return header;
    }
    private void setHeader(Element head) {
        this.header = head;
    }
    protected final Element getBody() {
        return body;
    }
    private void setBody(Element body) {
        this.body = body;
    }
}
